from urlparse import urlparse
from Business import Exploiter
from Business import PluginSQL
from Business import PluginXSS
from bs4 import BeautifulSoup

########################################################################
class Controller (object):
    """"""
    #----------------------------------------------------------------------
    def __init__(self, url):
        """
        Constructor
        """
        # Base da URL informada
        self.exploiter = Exploiter.Exploiter(url)
        self.URL_Original = self.exploiter.mount_URLs()
             

    #----------------------------------------------------------------------
    def check_URL (self):
        """
        Verifica se a URL infromada esta de acordo e funcional para ser utilizada
        """
        return self.exploiter.mount_URLs()
   
   
    #----------------------------------------------------------------------
    def get_Server_Info (self):
        """
        Retorna as informacoes do servidor
        """
        self.get_server_Info = self.exploiter.get_Server_Info()
        return self.get_server_Info

   
    #----------------------------------------------------------------------
    def select_Plugin (self, plugin, stop_scan):
        """
        Seleciona quais ataques devem ser realizados
        """
        self.stop_scan = stop_scan
       
        if plugin == 1:
            self.pl_attack = PluginSQL.PluginSQL()
        if plugin == 2:
            self.pl_attack = PluginXSS.PluginXSS()
         
         
   #----------------------------------------------------------------------
    def send_Exploiter (self):
        """
        Pega a URL informada, mais o plugin selecionado e retorna com o ataque
        """
        # Inclui as URLs na lista self.targets
        self.targets = []
        self.targets = self.exploiter.get_All_Targets()
        
        # Inclui os ataques na lista self.url_plugin
        self.url_plugin = []
        self.url_plugin = self.pl_attack.gerar_Ataques(self.targets)
        
        self.retorno_plugin = []
        
        for url_ataque in self.url_plugin:
            self.html = self.exploiter.send_Attack(url_ataque[0])
           
            self.retorno_plugin.append(self.pl_attack.analisa_Resultado(self.html, url_ataque[0], url_ataque[1]))
            self.resultado = self.pl_attack.analisa_Resultado(self.html, url_ataque[0], url_ataque[1])
            try:
                if (self.resultado[0] == 'VULNERAVEL') and (self.stop_scan == 1) and (self.resultado[0] != None):
                    return self.retorno_plugin
            except:
                pass
        
        
        #FORM ######
       
        self.ex_get_Form = []
        self.ex_get_Form = self.exploiter.get_Form(self.URL_Original)
        
        # Recebe uma lista com as URL, Formulario e Ataque
        self.pl_gen_Form_Attack = []
        self.pl_gen_Form_Attack = self.pl_attack.gen_Form_Attack(self.ex_get_Form, self.URL_Original)
        
        # Recebe uma lista com o codigo-fonte da pagina atacada
        self.respostas_html = []
        try:
            for n in self.pl_gen_Form_Attack:    
                self.respostas_html.append(self.exploiter.get_Form_HTML(n))
            
            for html in self.respostas_html:
                self.atributo_form = "FORMULARIOS: "
                for formu in html[0][1][1]:
                    self.atributo_form += ''.join(formu [0]) + "  "
                
                self.atributo_form += "ATAQUE: " + ''.join(formu [1])
                self.retorno_plugin.append(self.pl_attack.analisa_Resultado(html[0][0], html[0][1][0], self.atributo_form))  
                
            if (self.resultado[0] == 'VULNERAVEL') and (self.stop_scan == 1):
                return self.retorno_plugin                

        except:
            pass
        
        # FINAL FORM ######
        
        
        return self.retorno_plugin 
       
   